/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.tetris.gui;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import sirtet.event.BoardActionEvent;
import sirtet.event.BoardBlocksEliminatedEvent;
import sirtet.event.BoardEvent;
import sirtet.event.BoardListener;
import sirtet.event.BoardPieceEvent;
import sirtet.event.BoardPieceFixedEvent;
import sirtet.tetris.TetrisPieceFactory;

/**
 * Panel which displays the next coming piece, along with the label "Next Piece",
 * used by a TetrisBoardPanel.
 * @author guangwei.zhu
 */
public class NextPiecePanel extends JPanel implements BoardListener {

    public NextPiecePanel() {
        initComponents();
    }

    /**
     * This method initializes the layout of this Panel.  (This is not a NetBeans
     * generated function.)
     */
    protected void initComponents() {
        // Set Layout Manager
        GroupLayout layout = new GroupLayout(this);   // one column, any rows

        this.setLayout(layout);

        // Put a label saying "Next Piece"
        JLabel label = new JLabel("Next Piece");
        label.setFont(this.getFont().deriveFont(16.0f));
        label.setHorizontalAlignment(SwingConstants.CENTER);    // Center text
        label.setMaximumSize(new Dimension(100, 40));

        this.add(label);

        // Put a piece panel
        piecePanel = new TetrisPiecePanel();
        this.add(piecePanel);
        piecePanel.setPreferredSize(new Dimension(100, 100));

        layout.setHorizontalGroup(
                layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(label).addComponent(piecePanel)));
        layout.setVerticalGroup(
                layout.createSequentialGroup().addComponent(label).addComponent(piecePanel));

        // Test piece
        piecePanel.setSquareShaped(true);
        piecePanel.setHorizontalAlignment(SwingConstants.CENTER);
        piecePanel.setVerticalAlignment(SwingConstants.CENTER);
        piecePanel.setPieceColor(Color.RED);
        piecePanel.setPiece(new TetrisPieceFactory().fromString("1110100000000000"));
    }
    private TetrisPiecePanel piecePanel;

    @Override
    public void boardNextPieceChanged(BoardPieceEvent evt) {
        piecePanel.setPiece(evt.piece);
    }

    /* Unused interface slots */
    public void boardChanged(BoardEvent evt) {}
    public void boardGameOver(BoardEvent evt) {}
    public void boardStarted(BoardEvent evt) {}
    public void boardReset(BoardEvent evt) {}
    public void boardPieceFixed(BoardPieceFixedEvent evt) {}
    public void boardBlocksEliminated(BoardBlocksEliminatedEvent evt) {}
    public void boardActionTriggered(BoardActionEvent evt) {}
    public void boardDisposing(BoardEvent evt) {}
}
